package org.dromara.pe.controller;

import java.sql.Time;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.basic.domain.bo.TeachClassBo;
import org.dromara.basic.domain.vo.TeachClassVo;
import org.dromara.basic.service.ITeachClassService;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.pe.domain.bo.TaskReservationBo;
import org.dromara.pe.service.ITaskReservationService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.pe.domain.vo.TaskArrangeInfoVo;
import org.dromara.pe.domain.bo.TaskArrangeInfoBo;
import org.dromara.pe.service.ITaskArrangeInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 预约排班管理
 *
 * @author Lion Li
 * @date 2024-09-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pe/taskArrangeInfo")
public class TaskArrangeInfoController extends BaseController {

    private final ITaskArrangeInfoService taskArrangeInfoService;

    private final ITeachClassService teachClassService;
    /**
     * 查询预约排班管理列表
     */
    @SaCheckPermission("pe:taskArrangeInfo:list")
    @GetMapping("/list")
    public TableDataInfo<TaskArrangeInfoVo> list(TaskArrangeInfoBo bo, PageQuery pageQuery) {
        return taskArrangeInfoService.queryPageList(bo, pageQuery);
    }

    @SaCheckPermission("pe:taskArrangeInfo:list")
    @GetMapping("/reviewList")
    public TableDataInfo<TaskArrangeInfoVo> reviewList(TaskArrangeInfoBo bo, PageQuery pageQuery) {
        return taskArrangeInfoService.queryPageListReview(bo, pageQuery);
    }

    /**
     * 导出预约排班管理列表
     */
    @SaCheckPermission("pe:taskArrangeInfo:export")
    @Log(title = "预约排班管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TaskArrangeInfoBo bo, HttpServletResponse response) {
        List<TaskArrangeInfoVo> list = taskArrangeInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "预约排班管理", TaskArrangeInfoVo.class, response);
    }

    /**
     * 获取预约排班管理详细信息
     *
     * @param arrangeId 主键
     */
    @SaCheckPermission("pe:taskArrangeInfo:query")
    @GetMapping("/{arrangeId}")
    public R<TaskArrangeInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long arrangeId) {
        return R.ok(taskArrangeInfoService.queryById(arrangeId));
    }

    /**
     * 新增预约排班管理
     */
    @SaCheckPermission("pe:taskArrangeInfo:add")
    @Log(title = "预约排班管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TaskArrangeInfoBo bo) throws ParseException {
        return toAjax(taskArrangeInfoService.insertByBo(bo));
    }

    /**
     * 修改预约排班管理
     */
    @SaCheckPermission("pe:taskArrangeInfo:edit")
    @Log(title = "预约排班管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TaskArrangeInfoBo bo) throws ParseException {
        return toAjax(taskArrangeInfoService.updateByBo(bo));
    }

    /**
     * 删除预约排班管理
     *
     * @param arrangeIds 主键串
     */
    @SaCheckPermission("pe:taskArrangeInfo:remove")
    @Log(title = "预约排班管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{arrangeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] arrangeIds) {

        return toAjax(taskArrangeInfoService.deleteWithValidByIds(List.of(arrangeIds), true));
    }

    /**
     * 排班批量启用     *
     * @param statusByIds
     * @param status
     */
    @GetMapping("/{statusByIds}/{status}")
    public R<Void> updateStatusByIds(@NotEmpty(message = "主键不能为空")
                                          @PathVariable Long[] statusByIds , @PathVariable String status ) {
        if (statusByIds.length == 0 || StringUtils.isEmpty(status)) {  //入参不能为空
            return (toAjax(false));
        }
        return toAjax(taskArrangeInfoService.updateValidByIds(List.of(statusByIds), status,true));
    }
    @SaIgnore
    @RepeatSubmit()
    @PostMapping("/batch")
    public R<Void> batch(@RequestBody TaskArrangeInfoBo bo3) throws ParseException {
        List<TeachClassVo> teachClassVos = teachClassService.queryList(new TeachClassBo());
        for(TeachClassVo teachClassVo:teachClassVos){
            TaskArrangeInfoBo bo2=new TaskArrangeInfoBo();
            bo2.setClassId(teachClassVo.getClassId());
            List<TaskArrangeInfoVo> taskArrangeInfoVos = taskArrangeInfoService.queryList(bo2);
            if(CollectionUtils.isNotEmpty(taskArrangeInfoVos)){
                continue;
            }
            TaskArrangeInfoBo bo=new TaskArrangeInfoBo();
            bo.setClassId(teachClassVo.getClassId());
            bo.setArrangeName(teachClassVo.getClassName());
            bo.setScopeIds("20,21,22,23");
//            bo.setEndArrangeDate(new Date("2024-12-11 00:00:00"));
//            bo.setEndArrangeDate(new Date("2024-12-19 00:00:00"));
//            bo.setStartArrangeTime(new Time(""));
//            bo.setEndArrangeTime();
            bo.setSexLimit("2");
            bo.setArrangeNum(teachClassVo.getHeadcount());
            bo.setIsEnable("0");
            bo.setTaskId(47L);
            bo.setParams(bo3.getParams());
            Boolean b = taskArrangeInfoService.insertByBo(bo);

        }
        return toAjax(true);
    }
}
